home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / PlayerPRO 4.5.8 / PlayerPRO 4.5.8 Dev.Kit / Plug-Ins / Instruments Import⁄Export Plugs / AIFF.c next >
Encoding:
C/C++ Source or Header  |  1996-11-12  |  6.7 KB  |  311 lines  |  [TEXT/CWIE]

  1. /*    AIFF/AIFC        */
  2. /*  IMPORT/EXPORT    */
  3. /*    v 1.0            */
  4. /*    1996 by ANR        */
  5.  
  6. #include "PPPlug.h"
  7. #include "AIFF.h"
  8. #include "sound.h"
  9. #include "soundinput.h"
  10.  
  11. #if defined(powerc) || defined(__powerc)
  12. enum {
  13.         PlayerPROPlug = kCStackBased
  14.         | RESULT_SIZE(SIZE_CODE( sizeof(OSErr)))
  15.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
  16.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( InstrData*)))
  17.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( sData**)))
  18.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( short*)))
  19.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( FSSpec*)))
  20.         | STACK_ROUTINE_PARAMETER(6, SIZE_CODE(sizeof( PPInfoPlug*)))
  21. };
  22.  
  23. ProcInfoType __procinfo = PlayerPROPlug;
  24. #else
  25. #include <A4Stuff.h>
  26. #endif
  27.  
  28. Ptr         AIFFtoSnd(    Ptr                sound,
  29.                     long            *loopStart,
  30.                     long            *loopEnd,
  31.                     short            *sampleSize,
  32.                     unsigned long    *rate)
  33. {
  34. long                i, SizeH, numSampleFrames, StartId, numChannels;
  35. ContainerChunk        *CC;
  36. ChunkHeader            *CH;
  37. ExtCommonChunk        *CommC;
  38. SoundDataChunk        *SData;
  39. MarkerChunk            *marker;
  40. Marker                *mm;
  41. unsigned long        theFixed;
  42. Boolean                Compression = false;
  43. ID                    compType;
  44.  
  45.     CC = (ContainerChunk*) sound;
  46.  
  47.     *loopStart = *loopEnd = 0;
  48.     *sampleSize = 8;
  49.     *rate = rate22khz;
  50.     numChannels = 1;
  51.  
  52.     if( CC->formType == AIFCID) Compression = true;
  53.     else if( CC->formType == AIFFID) Compression = false;
  54.  
  55.     for( i = sizeof( ContainerChunk); i < CC->ckSize;)
  56.     {
  57.         CH = (ChunkHeader*) (sound + i);
  58.         if( CH->ckID == CommonID)
  59.         {
  60.             CommC = (ExtCommonChunk*) CH;            
  61.             numChannels = CommC->numChannels;
  62.             numSampleFrames = CommC->numSampleFrames;
  63.             compType = CommC->compressionType;
  64.             
  65.             {
  66.                 struct _extended80        *too;
  67.                 
  68.                 too = (struct _extended80* ) &CommC->sampleRate;
  69.                 
  70.                 *rate = *((unsigned long*) too->man);
  71.                 *rate >>= (15L - (too->exp - 0x3FFFL));
  72.             }
  73.             
  74.             *sampleSize = CommC->sampleSize;
  75.             if( *sampleSize == 16) numSampleFrames *= 2;
  76.  
  77.         }
  78.         else if( CH->ckID == SoundDataID)
  79.         {
  80.             SData = (SoundDataChunk*) CH;
  81.             StartId = i;
  82.         }
  83.         else if( CH->ckID == MarkerID)
  84.         {
  85.             marker = (MarkerChunk*) CH;
  86.             if( marker->numMarkers == 2)
  87.             {
  88.                 *loopStart = marker->Markers[ 0].position;
  89.                 mm = (Marker*) marker->Markers;
  90.                 mm = (Marker*) ((Ptr) mm + 8L);
  91.                 mm = (Marker*) ((Ptr) mm + marker->Markers[ 0].markerName[0]);
  92.                 *loopEnd = mm->position;
  93.                 
  94.                 if( *sampleSize == 16)
  95.                 {
  96.                     *loopStart *= 2;
  97.                     *loopEnd *= 2;
  98.                 }
  99.             }
  100.         }
  101.         
  102.         i += CH->ckSize;
  103.         i += 8;
  104.     }
  105.  
  106.     StartId += 16;          /* Size of the SoundDataChunk beginning */
  107.     
  108.     SizeH = GetPtrSize( sound);
  109.     if( numChannels == 1)
  110.     {
  111.         BlockMove( sound + StartId, sound, SizeH - StartId);
  112.     }
  113.     else
  114.     {
  115.         if( *sampleSize == 8)
  116.         {
  117.             for( i = 0; i < SizeH - StartId; i++)
  118.             {
  119.                 (sound)[ i] = (sound + StartId)[ i * numChannels];
  120.             }
  121.         }
  122.         else
  123.         {
  124.             for( i = 0; i < (SizeH - StartId)/2; i++)
  125.             {
  126.                 ((short*) sound)[ i] = ((short*) (sound + StartId))[ i * numChannels];
  127.             }
  128.         }
  129.     }
  130.     
  131.     if( Compression)
  132.     {
  133.         switch( compType)
  134.         {
  135.             case MACE3Type:
  136.                 sound = MyExp1to3( sound, numSampleFrames);
  137.                 numSampleFrames *= 3;
  138.                 
  139.                 ConvertInstrumentIn( (Byte*) sound, numSampleFrames);
  140.             break;
  141.             
  142.             case MACE6Type:
  143.                 sound = MyExp1to6( sound, numSampleFrames);
  144.                 numSampleFrames *= 6;
  145.                 
  146.                 ConvertInstrumentIn( (Byte*) sound, numSampleFrames);
  147.             break;
  148.             
  149.             default:
  150.                  return 0L;
  151.             break;
  152.         }
  153.     }
  154.     
  155.     SetPtrSize( sound, numSampleFrames);
  156.     
  157.     return sound;
  158. }
  159.  
  160. OSErr TestAIFF( ContainerChunk* CC)
  161. {
  162.     if( CC->formType == AIFCID) return noErr;
  163.     else if( CC->formType == AIFFID) return noErr;
  164.     else return MADFileNotSupportedByThisPlug;
  165. }
  166.  
  167. OSErr main(        OSType                    order,                        // Order to execute
  168.                 InstrData                *InsHeader,                    // Ptr on instrument header
  169.                 sData                    **sample,                    // Ptr on samples data
  170.                 short                    *sampleID,                    // If you need to replace/add only a sample, not replace the entire instrument (by example for 'AIFF' sound)
  171.                                                                     // If sampleID == -1 : add sample else replace selected sample.
  172.                 FSSpec                    *AlienFileFSSpec,            // IN/OUT file
  173.                 PPInfoPlug                *thePPInfoPlug)
  174. {
  175.     OSErr    myErr = noErr;
  176.     Ptr        AlienFile;
  177.     short    iFileRefI;
  178.     long    inOutBytes;
  179.     
  180.     #ifndef powerc
  181.         long    oldA4 = SetCurrentA4();             //this call is necessary for strings in 68k code resources
  182.     #endif
  183.     
  184.     switch( order)
  185.     {
  186.         case 'PLAY':
  187.         {
  188.             Ptr                theSound;
  189.             long            lS, lE;
  190.             short            sS;
  191.             unsigned long    rate;
  192.             
  193.             myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
  194.             if( myErr == noErr)
  195.             {
  196.                 GetEOF( iFileRefI, &inOutBytes);
  197.                 
  198.                 theSound = NewPtr( inOutBytes);
  199.                 if( theSound == 0L) myErr = MADNeedMemory;
  200.                 else
  201.                 {
  202.                     FSRead( iFileRefI, &inOutBytes, theSound);
  203.                     
  204.                     theSound = AIFFtoSnd( theSound, &lS, &lE, &sS, &rate);
  205.                     
  206.                     if( theSound != 0L)
  207.                     {
  208.                         myErr = CallRPlaySoundUPP( theSound, GetPtrSize( theSound), 0, 0xFF, sS, lS, lE, rate);
  209.                         
  210.                         DisposePtr( theSound);
  211.                         theSound = 0L;
  212.                     }
  213.                 }
  214.                 
  215.                 FSClose( iFileRefI);
  216.             }
  217.         }
  218.         break;
  219.     
  220.         case 'IMPL':
  221.         {
  222.             Ptr                theSound;
  223.             long            lS, lE;
  224.             short            sS;
  225.             unsigned long    rate;
  226.             
  227.             myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
  228.             if( myErr == noErr)
  229.             {
  230.                 GetEOF( iFileRefI, &inOutBytes);
  231.                 
  232.                 theSound = NewPtr( inOutBytes);
  233.                 if( theSound == 0L) myErr = MADNeedMemory;
  234.                 else
  235.                 {
  236.                     FSRead( iFileRefI, &inOutBytes, theSound);
  237.                     
  238.                     theSound = AIFFtoSnd( theSound, &lS, &lE, &sS, &rate);
  239.                     
  240.                     AddSoundToMAD( theSound, lS, lE, sS, 60, rate, AlienFileFSSpec->name, InsHeader, sample, sampleID);
  241.                 }
  242.                 
  243.                 FSClose( iFileRefI);
  244.             }
  245.         }
  246.         break;
  247.         
  248.         case 'TEST':
  249.         {
  250.             Ptr    theSound;
  251.             
  252.             myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
  253.             if( myErr == noErr)
  254.             {
  255.                 inOutBytes = 50L;
  256.                 theSound = NewPtr( inOutBytes);
  257.                 if( theSound == 0L) myErr = MADNeedMemory;
  258.                 else
  259.                 {
  260.                     FSRead( iFileRefI, &inOutBytes, theSound);
  261.                     
  262.                     myErr = TestAIFF( (ContainerChunk*) theSound);
  263.                 }
  264.                 
  265.                 DisposPtr( theSound);
  266.                 
  267.                 FSClose( iFileRefI);
  268.             }
  269.         }
  270.         break;
  271.         
  272.         case 'EXPL':
  273.             if( *sampleID >= 0)
  274.             {
  275.                 OSType            compType = 'NONE';
  276.                 unsigned long    rate;
  277.                 sData     *curData = sample[ *sampleID];
  278.                 
  279.                 myErr = FSpCreate( AlienFileFSSpec, 'TVOD', 'AIFF', smCurrentScript);
  280.                 myErr = FSpOpenDF( AlienFileFSSpec, fsCurPerm, &iFileRefI);
  281.                 
  282.                 if( myErr == noErr)
  283.                 {
  284.                     inOutBytes     = curData->size;
  285.                     rate        = curData->c2spd;
  286.                     
  287.                     myErr = SetupAIFFHeader(    iFileRefI,
  288.                                                 1,
  289.                                                 rate << 16L,
  290.                                                 curData->amp,
  291.                                                 compType,
  292.                                                 inOutBytes,
  293.                                                 0);
  294.                     
  295.                     myErr = FSWrite( iFileRefI, &inOutBytes, curData->data);
  296.                     FSClose( iFileRefI);
  297.                 }
  298.             }
  299.         break;
  300.         
  301.         default:
  302.             myErr = MADOrderNotImplemented;
  303.         break;
  304.     }
  305.     
  306.     #ifndef powerc
  307.         SetA4( oldA4);
  308.     #endif
  309.     
  310.     return myErr;
  311. }